package com.itheima.Interceptor;

import com.itheima.utils.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
@Slf4j
@Component
public class TokenInteceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //目标方法运行之前
        //获取到请求路径
     /*   String requestURI = request.getRequestURI();
        //判断是否是登录请求 请求路径包含/login
        if(requestURI.contains("/login")){
            log.info("登录请求,放行");

            return true;
        }*/

        //获取token
        String token = request.getHeader("token");

        //判断token是否存在 如果不存在说明用户没有登录直接返回错误信息
        if(token==null || token.isEmpty()){
            log.info("令牌为空");
            response.setStatus(401);
            return false;
        }

        //令牌存在校验令牌
        try {
            JwtUtils.parseToken(token);
        } catch (Exception e) {
            log.info("令牌不合法");
            return false;
        }

        //校验通过放行

        log.info("令牌校验通过");
        return true;
    }

    }

